package com.eric.service.impl;

import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.eric.domain.dto.ProjectDTO;
import com.eric.domain.po.Project;
import com.eric.domain.vo.ProjectVO;
import com.eric.mapper.ProjectMapper;
import com.eric.service.ProjectService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.update.UpdateChain;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

import static com.eric.domain.po.table.ProjectTableDef.PROJECT;


/**
 *  服务层实现。
 *
 * @author eric.deng
 * @since 2025-03-05
 */
@Service
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project>  implements ProjectService{
    @Override
    public SaResult updateByIdService(ProjectDTO projectDTO) {
        boolean projectNameEffective=false;
        boolean projectDescriptionEffective =false;
        if (!StrUtil.isBlank(projectDTO.getProjectName())){
            projectNameEffective=true;
        }
        if (!StrUtil.isBlank(projectDTO.getDescription())){
            projectDescriptionEffective=true;
        }

        Project project = queryChain().where(PROJECT.PROJECT_NAME.eq(StrUtil.trim(projectDTO.getProjectName()))).one();

        if (!BeanUtil.isEmpty(project)&& !Objects.equals(project.getId(),projectDTO.getId())){
            return SaResult.error("项目名称已经存在！");
        }

        boolean updated = UpdateChain.of(Project.class)
                .set(PROJECT.PROJECT_NAME, projectDTO.getProjectName(), projectNameEffective)
                .set(PROJECT.DESCRIPTION, projectDTO.getDescription(), projectDescriptionEffective)
                .where(PROJECT.ID.eq(projectDTO.getId()))
                .update();
        return SaResult.data(updated);
    }

    @Override
    public SaResult saveOneService(ProjectVO projectVO) {
        Project one = queryChain().select()
                .where(PROJECT.PROJECT_NAME.eq(projectVO.getProjectName()))
                .one();
        if (BeanUtil.isEmpty(one)){
            Project project=new Project();
            BeanUtil.copyProperties(projectVO,project);
            boolean save = save(project);
            return save ? SaResult.ok() : (SaResult.error("录入错误"));
        }else {
            return SaResult.error("项目已经存在！");
        }
    }

    @Override
    public SaResult getListByPageService(Integer cur, Integer pageSize) {
        if (cur==null||cur<=0){
            cur=1;
        }
        if (pageSize==null||pageSize<=0){
            pageSize=11;
        }
        List<Project> projectList = queryChain().select()
                .from(PROJECT).orderBy(PROJECT.UPDATE_TIME.desc())
                .page(new Page<>(cur, pageSize)).getRecords();
        if (!projectList.isEmpty()){
            List<ProjectDTO> projectDTOList = BeanUtil.copyToList(projectList, ProjectDTO.class);
            return SaResult.data(projectDTOList);
        }

        return SaResult.error("没有数据");
    }

    @Override
    public SaResult getProjectListByNameService(String name, Integer cur, Integer pageSize) {
        if (cur==null||cur<=0){
            cur=1;
        }
        if (pageSize==null||pageSize<=0){
            pageSize=11;
        }
        List<Project> projectList = queryChain().select()
                .where(PROJECT.PROJECT_NAME.like(name, true))
                .orderBy(PROJECT.UPDATE_TIME.desc())
                .page(new Page<>(cur, pageSize)).getRecords();

        if (!projectList.isEmpty()){
            List<ProjectDTO> projectDTOList = BeanUtil.copyToList(projectList, ProjectDTO.class);
            return SaResult.data(projectDTOList);
        }
        return SaResult.error("没有数据！");

    }

    @Override
    public SaResult getProjectListByNameCountService(String name) {
        long count = queryChain().select().where(PROJECT.PROJECT_NAME.like(name, true)).count();
        return SaResult.data(count);
    }
}
